package com.hh.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.commonutils.Result;
import com.hh.commonutils.jwt.JwtUtil;
import com.hh.eduservice.entity.EduCourse;
import com.hh.eduservice.entity.EduCourseDescription;
import com.hh.eduservice.entity.EduSubject;
import com.hh.eduservice.entity.EduTeacher;
import com.hh.eduservice.entity.chapter.ChapterVo;
import com.hh.eduservice.entity.vo.CourseVo;
import com.hh.eduservice.entity.vo.PublishVo;
import com.hh.eduservice.mapper.*;
import com.hh.eduservice.service.EduChapterService;
import com.hh.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.eduservice.service.EduTeacherService;
import com.hh.eduservice.service.client.serviceOrderClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author huhuan
 * @since 2021-10-20
 */
@Service
@Transactional
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseMapper courseMapper;
    @Autowired
    private EduCourseDescriptionMapper eduCourseDescriptionMapper;
    @Autowired
    private com.hh.eduservice.service.client.serviceOrderClient serviceOrderClient;
    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String addCourse(CourseVo courseVo) {
        // 添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseVo, eduCourse);
        int insert = courseMapper.insert(eduCourse);

        // 添加课程描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseVo.getDescription());
        int insert1 = eduCourseDescriptionMapper.insert(eduCourseDescription);
        if (insert == 0 || insert1 == 0) {
            throw new RuntimeException("添加课程信息失败！");
        }
        return eduCourse.getId();
    }


    @Override
    public HashMap<String, Object> getCourseById(String id, String memberId) {
        // 1、查询课程信息
        CourseVo vo = courseMapper.getFrontCourseVoById(id);

        // 2、查询章节、视频信息
//        List<ChapterVo> chapterById = eduChapterMapper.getChapterAndVideoInfoById(id);
        List<ChapterVo> chapterById = eduChapterService.getChapterById(id);

        int status;
        if (!StringUtils.isEmpty(memberId)) {
            status = serviceOrderClient.getOrderInfoStatus(id,memberId);
        } else {
            status = 0;
        }
        vo.setIsBuy(status==1);

        HashMap<String, Object> map = new HashMap<>();
        map.put("courseVo", vo);
        map.put("courseWebVo", vo);
        map.put("chapterVideoList", chapterById);

        return map;
    }

    @Override
    @Transactional
    public String updateCourse(CourseVo courseVo) {
        EduCourse course = new EduCourse();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseVo, course);
        courseMapper.updateById(course);

        eduCourseDescription.setId(courseVo.getId());
        eduCourseDescription.setDescription(courseVo.getDescription());
        eduCourseDescriptionMapper.updateById(eduCourseDescription);

        return courseVo.getId();
    }

    @Override
    public PublishVo getPublishMessage(String courseId) {
        PublishVo publishVo = courseMapper.getPublishMessageByCourseId(courseId);
        return publishVo;
    }

    @Override
    public void publishCourse(String id) {
        // 发布课程，修改状态为normal
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseMapper.updateById(eduCourse);
    }

    @Override
    public ArrayList getLimitEduCours() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 8");
        ArrayList<EduCourse> list = (ArrayList<EduCourse>) courseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<EduCourse> getCourseByTeacherId(String id) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", id);
        wrapper.orderByDesc("gmt_create");
        List<EduCourse> courses = courseMapper.selectList(wrapper);
        return courses;
    }

    @Override
    public HashMap<String, Object> getFrontCourseList(Page pageParam,CourseVo courseVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(courseVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id", courseVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseVo.getSubjectParentId()) && !StringUtils.isEmpty(courseVo.getSubjectId())) {
            wrapper.eq("subject_id", courseVo.getSubjectId());
        } else if (!StringUtils.isEmpty(courseVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        } else if (!StringUtils.isEmpty(courseVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        } else {
            wrapper.orderByDesc("gmt_create");
        }


        courseMapper.selectPage(pageParam, wrapper);
        long current = pageParam.getCurrent();
        List<EduCourse> items = pageParam.getRecords();
        long total = pageParam.getTotal();
        long pages = pageParam.getPages();
        boolean hasPrevious = pageParam.hasPrevious();
        boolean hasNext = pageParam.hasNext();

        HashMap<String, Object> result = new HashMap<>();
        result.put("current",current);
        result.put("total", total);
        result.put("items", items);
        result.put("pages", pages);
        result.put("hasPrevious", hasPrevious);
        result.put("hasNext", hasNext);
        return result;
    }

}
